package
{
	import away3d.cameras.Camera3D;
	import away3d.containers.View3D;
	import away3d.core.base.Face;
	import away3d.core.base.Mesh;
	import away3d.core.base.Segment;
	import away3d.core.base.UV;
	import away3d.core.base.Vertex;
	import away3d.core.clip.Clipping;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.Material;
	import away3d.materials.WireframeMaterial;
	import away3d.primitives.Cube;
	import away3d.primitives.LineSegment;
	import away3d.primitives.Plane;
	import away3d.primitives.Sphere;
	import away3d.primitives.Trident;
	import away3d.sprites.Sprite3D;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event; 
	import flash.events.MouseEvent;
	
	public class Main extends Sprite
	{
		private var view:View3D;
		
		
		[Embed(source="../bin/student.csv", mimeType="application/octet-stream")]
		private var csvText:Class;
		
		[Embed(source="../bin/texture/iscript.png")]
		private var iscriptImage:Class;
		
		private	var  sphere:Sphere  =  new  Sphere();
		
		private var  plane:Plane = new Plane();
		private var t:Trident = new Trident(200);
		public function Main():void
		{
			view = new View3D()
	/*		view.clipping = new Clipping();
			view.clipping.minX = 100;
			view.clipping.minY = 100;
			view.clipping.maxX = 400;
			view.clipping.minY = 400;*/
			
			view.x = stage.stageWidth / 2;
			view.y = stage.stageHeight / 2;
			//view.camera = new Camera3D();
			
		//view.camera.fov = 0;
		
			view.camera.focus = 1;
			view.camera.zoom = 1000;
			
			view.camera.z = -1000;
			
			//view.camera.pan (5);
			//view.camera.yaw(5)
			
			//view.camera.focus = 600/2*Math.cos(view.camera.fov/2)/Math.sin(view.camera.fov/2)
			
			addChild (view);
			//float focalLength = Convert.ToSingle(ViewPort.Height2 * Math.Cos(Fov / 2) / Math.Sin(Fov / 2));
			//trace(view.camera.focus)
			//view.camera.focus = 
			
			//trace(view.camera.fov)
			
			
			var image:Bitmap = new iscriptImage() as Bitmap;
			//var bmp:BitmapData = new BitmapData(200,200);
			//bmp.perlinNoise(200, 200, 2, Math.random(), true, true);
			var mat:BitmapMaterial = new BitmapMaterial(image.bitmapData);
			
		
		//	sphere.material =  mat;
			sphere.segmentsW = 50;
			sphere.segmentsH = 50;
		//	sphere.x = -200;
			sphere.z = 90;
	 
		 	//view.scene.addChild (sphere);
		
			var bet:int =8;
			
			var mesh:Mesh = new Mesh();
		 	var face:Face = new Face(
			     new Vertex(0, 10*bet, 0),
				 new Vertex( -10*bet, -10*bet, 0),
				 new Vertex(0, -10*bet, 0),
				 mat,
				 new UV(0, 0),
				 new UV(0, 1),
				 new UV(1,0)
				 );
		 	mesh.addFace(face);
			
			
		//	view.scene.addChild(mesh);			
			var sprite3d:Sprite3D = new Sprite3D(mat)
			sprite3d.width = image.width;
			sprite3d.height = image.height;
		 //	mesh.addSprite(sprite3d);
	 
			
	 	plane.width = image.width;
	 	plane.height = image.height;
		plane.material = new WireframeMaterial();
		plane.rotationX = 90;
		plane.bothsides = true;
		plane.material = mat;
		//trace(plane.objectWidth, plane.objectHeight);
		//trace("plane.z",plane.z)
		
		     view.scene.addChild(plane);
		
            
			view.scene.addChild (t);
			
			
	 		var line:Segment = new Segment(
			     new Vertex( -100, 70, 0),
			     new Vertex( -20, -20, 0)
			  )
			  
			  var lineMaterial:WireframeMaterial = new WireframeMaterial(0xA5EB65)
			  line.material = lineMaterial
			  mesh.addSegment(line);
			
			
			view.scene.addChild (mesh); 
			
			
			var cube:Cube = new Cube ();
			     
			
			view.scene.addChild(cube);
			
			
			
			
		//	sphere.moveBackward(0)
			
		stage.addEventListener(MouseEvent.CLICK,xxx)
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function xxx(e:MouseEvent):void 
		{
			//if(sphere.z!=0)
			//sphere.moveBackward(0-sphere.z)
		}
		
		private function onEnterFrame(ev:Event):void
		{
			//t.rotationY++;
			
			//view.camera.rotationY++;
			
			//plane.z = (view.camera.zoom * view.camera.focus) - Math.abs(view.camera.z);
			view.render ();
		}
	
	}

}

